#include "mecameramanager.h"
#include <QDebug>
#include <QMutex>

MeCameraManager* MeCameraManager::m_instance = nullptr;

MeCameraManager::MeCameraManager(QObject* parent)
    : QObject(parent)
{
    m_worker = new MeCameraWorker();
    m_worker->moveToThread(&m_workerThread);

    // 连接信号槽
    connect(this, &MeCameraManager::initializeRequested, m_worker, &MeCameraWorker::initialize);
    connect(this, &MeCameraManager::startAcquisitionRequested, m_worker, &MeCameraWorker::startAcquisition);
    connect(this, &MeCameraManager::stopAcquisitionRequested, m_worker, &MeCameraWorker::stopAcquisition);
    connect(this, &MeCameraManager::triggerSingleShotRequested, m_worker, &MeCameraWorker::triggerSingleShot);
    connect(this, &MeCameraManager::disconnectCameraRequested, m_worker, &MeCameraWorker::disconnectCamera);
    connect(this, &MeCameraManager::connectCameraRequested, m_worker, &MeCameraWorker::connectCamera);
    connect(this, &MeCameraManager::enumerateDevicesRequested, m_worker, [this]() {
        s_DevicesInfo info;
        CMeCamera tempCamera;
        if (tempCamera.EnumDevices(info)) {
            qDebug() << "Found" << info.iNum << "cameras";
            for (int i = 0; i < info.iNum; i++) {
                qDebug() << "Camera" << i << ":";
                qDebug() << "  SN:" << QString::fromStdString(info.sCameraInfo[i].SN);
                qDebug() << "  Model:" << QString::fromStdString(info.sCameraInfo[i].sModelName);
                qDebug() << "  IP:" << QString::fromStdString(info.sCameraInfo[i].IpAddr);
                qDebug() << "  UserID:" << QString::fromStdString(info.sCameraInfo[i].sDeviceUserID);
            }
            emit devicesEnumerated(info);
        } else {
            emit errorOccurred("Camera enumeration failed");
        }
    });

    // 连接worker信号到manager信号
    connect(m_worker, &MeCameraWorker::cameraOpened, this, &MeCameraManager::cameraOpened);
    connect(m_worker, &MeCameraWorker::cameraClosed, this, &MeCameraManager::cameraClosed);
    connect(m_worker, &MeCameraWorker::acquisitionStarted, this, &MeCameraManager::acquisitionStarted);
    connect(m_worker, &MeCameraWorker::acquisitionStopped, this, &MeCameraManager::acquisitionStopped);
    connect(m_worker, &MeCameraWorker::frameAcquired, this, &MeCameraManager::frameAcquired);
    connect(m_worker, &MeCameraWorker::errorOccurred, this, &MeCameraManager::errorOccurred);
    connect(m_worker, &MeCameraWorker::devicesEnumerated, this, &MeCameraManager::devicesEnumerated);

    m_workerThread.start();
}

MeCameraManager::~MeCameraManager()
{
    m_workerThread.quit();
    m_workerThread.wait();
    delete m_worker;
}

MeCameraManager* MeCameraManager::getInstance()
{
    static QMutex mutex;
    QMutexLocker locker(&mutex);

    if (!m_instance) {
        m_instance = new MeCameraManager();
    }
    return m_instance;
}

void MeCameraManager::initialize()
{
    emit initializeRequested();
}

void MeCameraManager::connectCamera(const QString& ipAddress)
{
    emit connectCameraRequested(ipAddress);
}

void MeCameraManager::startContinuousAcquisition(int intervalMs)
{
    emit startAcquisitionRequested(intervalMs);
}

void MeCameraManager::stopAcquisition()
{
    emit stopAcquisitionRequested();
}

void MeCameraManager::triggerSingleShot()
{
    emit triggerSingleShotRequested();
}

void MeCameraManager::disconnectCamera()
{
    emit disconnectCameraRequested();
}

void MeCameraManager::enumerateDevices()
{
    emit enumerateDevicesRequested();
}

bool MeCameraManager::isCameraConnected() const
{
    return m_worker->isCameraConnected();
}


bool MeCameraManager::isAcquiring() const
{
    return m_worker->isAcquiring();
}

QString MeCameraManager::getCameraStatus() const
{
    if (isAcquiring()) return "采集中";
    if (isCameraConnected()) return "已连接";
    return "未连接";
}
